﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBScript;
using eBlog.Cache;
using eBlog.Common;
using eBlog.Common.Files;
using eBlog.Entity;
using eBlog.Core;
using eBlog.Common.Security;
using System.Web;
namespace eBlog.Web.Admin
{
    public partial class Login : PageBase
    {

        IDBBase dal = DBFactory.GetDbProvider();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                switch (action)
                {
                    case "quit":

                        try
                        {
                             DataCache.RemoveCache("Role_" + Request.Cookies["eBlogAdmin"].Values["name"]);
                        }
                        catch { }

                        HttpCookie AdminCookie = new HttpCookie("eBlogAdmin") {Expires = DateTime.Now.AddMinutes(-30)};
                        Response.Cookies.Add(AdminCookie);
                        
                        break;
                }
            }




        }

  

        public void btnLogin_Click(object sender, EventArgs e)
        {
            ArrayList strErrs = new ArrayList(); //Add时候必须用::分开控件名

            if (String.IsNullOrEmpty(PageValidate.Form("admin_username")))
            {
                strErrs.Add("admin_username::管理员不能为空，请输入管理员名称！");
            }
            if (String.IsNullOrEmpty(PageValidate.Form("admin_password")))
            {
                strErrs.Add("admin_password::密码不能为空，请输入管理员密码！");
            }
            if (String.IsNullOrEmpty(PageValidate.Form("admin_safecode")))
            {
                strErrs.Add("admin_safecode::安全码不能为空，请输入安全码！");
            }
          

            if (strErrs.Count > 0)
            {
                string[] arrayErrs = (string[])strErrs.ToArray(typeof(string));
                string strErr = string.Join("\\n", arrayErrs);
                //MessageBox.Show(this,strErr);
               // ErrCodeStr.Value = strErr;
                return;
            }

            string LoginName = PageValidate.Form("admin_username");
            string PassWord = MD5.Encrypt( PageValidate.Form("admin_password"));
            string safecode = PageValidate.Form("admin_safecode");
            
            if(safecode!=ConfigHelper.SiteKey)
            {
                MessageBox.Show(this, "管理员或密码或安全码错误!");
            }

            Entity.Admin model = dal.From<Entity.Admin>().Where("LoginName=@LoginName and PassWord=@PassWord")
                 .SetString("LoginName", LoginName)
                 .SetString("PassWord", PassWord)
                 .GetModel<Entity.Admin>();




             if (model != null)
             {


                 model.LastLoginIP = Request.UserHostAddress;

                 if (model.LoginCount == null)
                     model.LoginCount = 0;
                 else
                    model.LoginCount = model.LoginCount + 1;

                 model.LastLoginTime = TimeParser.GetNowTime();
                 model.TableName = "Admin";
                 dal.Update(model);   //更新最后登录信息

                 //记录cookie
                 HttpCookie AdminCookie = new HttpCookie("eBlogAdmin");
                 AdminCookie["id"] = model.ID.ToString();
                 AdminCookie["name"] = model.LoginName;
                 AdminCookie["adminname"] = Server.UrlEncode(model.AdminName);
                 AdminCookie["key"] = MD5.FormatCookie(model.LoginName);
                 AdminCookie.Expires = DateTime.Now.AddMinutes(30);
                 Response.Cookies.Add(AdminCookie);

                 //缓存权限url

                 //if (!DataCache.Exists("Role_" + model.LoginName))
                 //{ 
                    
                    
                 //}
                 //AdminRole role = dal.GetModel<AdminRole>(model.RoleID);
                 //if (role != null)
                 //{
                 //    DataCache.SetCache("Role_" + model.LoginName, role.RightsUrl);
                 //}

                  Response.Redirect("index.aspx");


             }
             else
             {

                 MessageBox.Show(this, "管理员或密码错误!");
                 return;

             }




           
        }

   


    }
}

